{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Keras 学习入门 #"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在过年的几天之内，我接触了深度学习框架keras，相对于以前接触的学习框架，theano，lasagne等，这个框架集成度更高，请大家与我一起来学习这篇入门课程，将带领大家一起来学习keras的框架，一定不会令大家失望。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 首先介绍一下我的机器环境 ###"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Anaconda \n",
    "2. keras 0.3.2\n",
    "3. jupyter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import keras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.3.2'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keras.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 这篇教程使用cifra10 作为教程，一步一步实现使用keras实现识别的功能 ####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先导入必要的库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.datasets import cifar10\n",
    "from keras.utils import np_utils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "nb_classes = 10\n",
    "batch_size = 32\n",
    "nb_epoch = 200\n",
    "img_rows,img_cols = 32,32\n",
    "img_channels = 3\n",
    "def load_dataset():\n",
    "    # 读取数据集\n",
    "    (X_train,y_train),(X_test,y_test) = cifar10.load_data()\n",
    "    print 'X_train shape:',X_train.shape\n",
    "    print X_train.shape[0],'train samples'\n",
    "    print X_test.shape[0],'test samples'\n",
    "    \n",
    "    # 将类别转化为向量\n",
    "    Y_train = np_utils.to_categorical(y_train,nb_classes)\n",
    "    Y_test = np_utils.to_categorical(y_test,nb_classes)\n",
    "    \n",
    "    X_train = X_train.astype('float32')\n",
    "    X_test = X_test.astype('float32')\n",
    "    X_train /= 255\n",
    "    X_test /=255\n",
    "    \n",
    "    return X_train,Y_train,X_test,Y_test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**下面来构建神经网络**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using Theano backend.\n",
      "Using gpu device 0: GeForce GT 635M (CNMeM is disabled)\n",
      "D:\\Anaconda\\lib\\site-packages\\theano-0.8.0.dev0-py2.7.egg\\theano\\tensor\\signal\\downsample.py:5: UserWarning: downsample module has been moved to the pool module.\n",
      "  warnings.warn(\"downsample module has been moved to the pool module.\")\n"
     ]
    }
   ],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers.core import Dense, Dropout, Activation, Flatten\n",
    "from keras.layers.convolutional import Convolution2D, MaxPooling2D\n",
    "from keras.optimizers import SGD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def make_network():\n",
    "    model = Sequential()\n",
    "    model.add(Convolution2D(32,3,3,border_mode='same',input_shape=(img_channels,img_rows,img_cols)))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Convolution2D(32,3,3))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2,2)))\n",
    "    model.add(Dropout(0.25))\n",
    "    \n",
    "    model.add(Convolution2D(64,3,3,border_mode='same'))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Convolution2D(64,3,3))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2,2)))\n",
    "    model.add(Dropout(0.25))\n",
    "    \n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(512))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Dense(nb_classes))\n",
    "    model.add(Activation('softmax'))\n",
    "    \n",
    "    return model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "开始训练这个模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def train_model(model,X_train,Y_train,X_test,Y_test):\n",
    "    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
    "    model.compile(loss='categorical_crossentropy', optimizer=sgd)\n",
    "    model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size,\n",
    "             validation_split=0.1, show_accuracy=True, verbose=1)\n",
    "    print('Testing...')\n",
    "    res = model.evaluate(X_test, Y_test,\n",
    "                        batch_size=batch_size, verbose=1, show_accuracy=True)\n",
    "    print('Test accuracy: {0}'.format(res[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X_train shape: (50000L, 3L, 32L, 32L)\n",
      "50000 train samples\n",
      "10000 test samples\n"
     ]
    }
   ],
   "source": [
    "X_train,Y_train,X_test,Y_test = load_dataset()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = make_network()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 45000 samples, validate on 5000 samples\n",
      "Epoch 1/200\n",
      "45000/45000 [==============================] - 354s - loss: 1.6543 - acc: 0.3944 - val_loss: 1.2448 - val_acc: 0.5466\n",
      "Epoch 2/200\n",
      "45000/45000 [==============================] - 355s - loss: 1.2764 - acc: 0.5420 - val_loss: 1.1139 - val_acc: 0.6176\n",
      "Epoch 3/200\n",
      "45000/45000 [==============================] - 358s - loss: 1.1241 - acc: 0.6000 - val_loss: 0.9491 - val_acc: 0.6714\n",
      "Epoch 4/200\n",
      "45000/45000 [==============================] - 359s - loss: 1.0254 - acc: 0.6396 - val_loss: 0.8924 - val_acc: 0.6888\n",
      "Epoch 5/200\n",
      "45000/45000 [==============================] - 364s - loss: 0.9468 - acc: 0.6660 - val_loss: 0.8758 - val_acc: 0.6990\n",
      "Epoch 6/200\n",
      "45000/45000 [==============================] - 357s - loss: 0.8973 - acc: 0.6837 - val_loss: 0.8283 - val_acc: 0.7082\n",
      "Epoch 7/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.8591 - acc: 0.7003 - val_loss: 0.7813 - val_acc: 0.7266\n",
      "Epoch 8/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.8278 - acc: 0.7081 - val_loss: 0.7216 - val_acc: 0.7496\n",
      "Epoch 9/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.7900 - acc: 0.7225 - val_loss: 0.7140 - val_acc: 0.7544\n",
      "Epoch 10/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.7662 - acc: 0.7311 - val_loss: 0.6950 - val_acc: 0.7602\n",
      "Epoch 11/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.7488 - acc: 0.7362 - val_loss: 0.7290 - val_acc: 0.7442\n",
      "Epoch 12/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.7358 - acc: 0.7425 - val_loss: 0.7120 - val_acc: 0.7562\n",
      "Epoch 13/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.7079 - acc: 0.7524 - val_loss: 0.7126 - val_acc: 0.7630\n",
      "Epoch 14/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.7001 - acc: 0.7572 - val_loss: 0.7277 - val_acc: 0.7550\n",
      "Epoch 15/200\n",
      "45000/45000 [==============================] - 357s - loss: 0.6906 - acc: 0.7577 - val_loss: 0.6809 - val_acc: 0.7692\n",
      "Epoch 16/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.6808 - acc: 0.7613 - val_loss: 0.7041 - val_acc: 0.7584\n",
      "Epoch 17/200\n",
      "45000/45000 [==============================] - 359s - loss: 0.6643 - acc: 0.7692 - val_loss: 0.7119 - val_acc: 0.7570\n",
      "Epoch 18/200\n",
      "45000/45000 [==============================] - 362s - loss: 0.6602 - acc: 0.7678 - val_loss: 0.7407 - val_acc: 0.7548\n",
      "Epoch 19/200\n",
      "45000/45000 [==============================] - 362s - loss: 0.6545 - acc: 0.7732 - val_loss: 0.7227 - val_acc: 0.7586\n",
      "Epoch 20/200\n",
      "45000/45000 [==============================] - 364s - loss: 0.6494 - acc: 0.7744 - val_loss: 0.6938 - val_acc: 0.7668\n",
      "Epoch 21/200\n",
      "45000/45000 [==============================] - 362s - loss: 0.6444 - acc: 0.7783 - val_loss: 0.7048 - val_acc: 0.7678\n",
      "Epoch 22/200\n",
      "45000/45000 [==============================] - 359s - loss: 0.6336 - acc: 0.7822 - val_loss: 0.6786 - val_acc: 0.7742\n",
      "Epoch 23/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.6286 - acc: 0.7829 - val_loss: 0.6907 - val_acc: 0.7710\n",
      "Epoch 24/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.6304 - acc: 0.7815 - val_loss: 0.6624 - val_acc: 0.7748\n",
      "Epoch 25/200\n",
      "45000/45000 [==============================] - 424s - loss: 0.6084 - acc: 0.7900 - val_loss: 0.7111 - val_acc: 0.7724\n",
      "Epoch 26/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.6269 - acc: 0.7836 - val_loss: 0.7040 - val_acc: 0.7712\n",
      "Epoch 27/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.6167 - acc: 0.7884 - val_loss: 0.6724 - val_acc: 0.7748\n",
      "Epoch 28/200\n",
      "45000/45000 [==============================] - 361s - loss: 0.6012 - acc: 0.7908 - val_loss: 0.7377 - val_acc: 0.7608\n",
      "Epoch 29/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.6102 - acc: 0.7903 - val_loss: 0.7233 - val_acc: 0.7620\n",
      "Epoch 30/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.6050 - acc: 0.7921 - val_loss: 0.7238 - val_acc: 0.7678\n",
      "Epoch 31/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5998 - acc: 0.7953 - val_loss: 0.6975 - val_acc: 0.7692\n",
      "Epoch 32/200\n",
      "45000/45000 [==============================] - 358s - loss: 0.5976 - acc: 0.7945 - val_loss: 0.6666 - val_acc: 0.7834\n",
      "Epoch 33/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5859 - acc: 0.8006 - val_loss: 0.6884 - val_acc: 0.7726\n",
      "Epoch 34/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5964 - acc: 0.7975 - val_loss: 0.6701 - val_acc: 0.7842\n",
      "Epoch 35/200\n",
      "45000/45000 [==============================] - 358s - loss: 0.5881 - acc: 0.8006 - val_loss: 0.7004 - val_acc: 0.7704\n",
      "Epoch 36/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5848 - acc: 0.8009 - val_loss: 0.7034 - val_acc: 0.7772\n",
      "Epoch 37/200\n",
      "45000/45000 [==============================] - 358s - loss: 0.5778 - acc: 0.8017 - val_loss: 0.7095 - val_acc: 0.7738\n",
      "Epoch 38/200\n",
      "45000/45000 [==============================] - 358s - loss: 0.5755 - acc: 0.8049 - val_loss: 0.6896 - val_acc: 0.7798\n",
      "Epoch 39/200\n",
      "45000/45000 [==============================] - 357s - loss: 0.6025 - acc: 0.7956 - val_loss: 0.6864 - val_acc: 0.7762\n",
      "Epoch 40/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5848 - acc: 0.8024 - val_loss: 0.7035 - val_acc: 0.7706\n",
      "Epoch 41/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5721 - acc: 0.8074 - val_loss: 0.6868 - val_acc: 0.7808\n",
      "Epoch 42/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5829 - acc: 0.8030 - val_loss: 0.6769 - val_acc: 0.7774\n",
      "Epoch 43/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5744 - acc: 0.8041 - val_loss: 0.7178 - val_acc: 0.7676\n",
      "Epoch 44/200\n",
      "45000/45000 [==============================] - 353s - loss: 0.5744 - acc: 0.8066 - val_loss: 0.7895 - val_acc: 0.7514\n",
      "Epoch 45/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5790 - acc: 0.8046 - val_loss: 0.7134 - val_acc: 0.7694\n",
      "Epoch 46/200\n",
      "45000/45000 [==============================] - 357s - loss: 0.5862 - acc: 0.8021 - val_loss: 0.7114 - val_acc: 0.7740\n",
      "Epoch 47/200\n",
      "45000/45000 [==============================] - 359s - loss: 0.5827 - acc: 0.8050 - val_loss: 0.7358 - val_acc: 0.7592\n",
      "Epoch 48/200\n",
      "45000/45000 [==============================] - 371s - loss: 0.5852 - acc: 0.8035 - val_loss: 0.7556 - val_acc: 0.7530\n",
      "Epoch 49/200\n",
      "45000/45000 [==============================] - 397s - loss: 0.5732 - acc: 0.8090 - val_loss: 0.6931 - val_acc: 0.7794\n",
      "Epoch 50/200\n",
      "45000/45000 [==============================] - 399s - loss: 0.5629 - acc: 0.8124 - val_loss: 0.7466 - val_acc: 0.7694\n",
      "Epoch 51/200\n",
      "45000/45000 [==============================] - 406s - loss: 0.5913 - acc: 0.8026 - val_loss: 0.7530 - val_acc: 0.7458\n",
      "Epoch 52/200\n",
      "45000/45000 [==============================] - 420s - loss: 0.5725 - acc: 0.8082 - val_loss: 0.7486 - val_acc: 0.7698\n",
      "Epoch 53/200\n",
      "45000/45000 [==============================] - 413s - loss: 0.5762 - acc: 0.8084 - val_loss: 0.7148 - val_acc: 0.7746\n",
      "Epoch 54/200\n",
      "45000/45000 [==============================] - 387s - loss: 0.5769 - acc: 0.8057 - val_loss: 0.7403 - val_acc: 0.7612\n",
      "Epoch 55/200\n",
      "45000/45000 [==============================] - 420s - loss: 0.5705 - acc: 0.8082 - val_loss: 0.6875 - val_acc: 0.7792\n",
      "Epoch 56/200\n",
      "45000/45000 [==============================] - 430s - loss: 0.5684 - acc: 0.8093 - val_loss: 0.7284 - val_acc: 0.7660\n",
      "Epoch 57/200\n",
      "45000/45000 [==============================] - 426s - loss: 0.5700 - acc: 0.8104 - val_loss: 0.7178 - val_acc: 0.7686\n",
      "Epoch 58/200\n",
      "45000/45000 [==============================] - 421s - loss: 0.5557 - acc: 0.8140 - val_loss: 0.6927 - val_acc: 0.7874\n",
      "Epoch 59/200\n",
      "45000/45000 [==============================] - 376s - loss: 0.5673 - acc: 0.8111 - val_loss: 0.7232 - val_acc: 0.7688\n",
      "Epoch 60/200\n",
      "45000/45000 [==============================] - 387s - loss: 0.5820 - acc: 0.8066 - val_loss: 0.7380 - val_acc: 0.7638\n",
      "Epoch 61/200\n",
      "45000/45000 [==============================] - 393s - loss: 0.5558 - acc: 0.8155 - val_loss: 0.6931 - val_acc: 0.7826\n",
      "Epoch 62/200\n",
      "45000/45000 [==============================] - 404s - loss: 0.5582 - acc: 0.8131 - val_loss: 0.7107 - val_acc: 0.7782\n",
      "Epoch 63/200\n",
      "45000/45000 [==============================] - 408s - loss: 0.5669 - acc: 0.8124 - val_loss: 0.7047 - val_acc: 0.7724\n",
      "Epoch 64/200\n",
      "45000/45000 [==============================] - 400s - loss: 0.5609 - acc: 0.8131 - val_loss: 0.7057 - val_acc: 0.7764\n",
      "Epoch 65/200\n",
      "45000/45000 [==============================] - 442s - loss: 0.5491 - acc: 0.8178 - val_loss: 0.7087 - val_acc: 0.7802\n",
      "Epoch 66/200\n",
      "45000/45000 [==============================] - 458s - loss: 0.5711 - acc: 0.8102 - val_loss: 0.7279 - val_acc: 0.7636\n",
      "Epoch 67/200\n",
      "45000/45000 [==============================] - 450s - loss: 0.5562 - acc: 0.8157 - val_loss: 0.7149 - val_acc: 0.7730\n",
      "Epoch 68/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5531 - acc: 0.8198 - val_loss: 0.7605 - val_acc: 0.7538\n",
      "Epoch 69/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5589 - acc: 0.8153 - val_loss: 0.7184 - val_acc: 0.7736\n",
      "Epoch 70/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5482 - acc: 0.8205 - val_loss: 0.7016 - val_acc: 0.7840\n",
      "Epoch 71/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5471 - acc: 0.8214 - val_loss: 0.7677 - val_acc: 0.7722\n",
      "Epoch 72/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5590 - acc: 0.8163 - val_loss: 0.7425 - val_acc: 0.7692\n",
      "Epoch 73/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5540 - acc: 0.8159 - val_loss: 0.7500 - val_acc: 0.7682\n",
      "Epoch 74/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5579 - acc: 0.8163 - val_loss: 0.7646 - val_acc: 0.7648\n",
      "Epoch 75/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5494 - acc: 0.8202 - val_loss: 0.7178 - val_acc: 0.7710\n",
      "Epoch 76/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5446 - acc: 0.8197 - val_loss: 0.7472 - val_acc: 0.7556\n",
      "Epoch 77/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5454 - acc: 0.8209 - val_loss: 0.7278 - val_acc: 0.7782\n",
      "Epoch 78/200\n",
      "45000/45000 [==============================] - 358s - loss: 0.5430 - acc: 0.8211 - val_loss: 0.7248 - val_acc: 0.7770\n",
      "Epoch 79/200\n",
      "45000/45000 [==============================] - 357s - loss: 0.5549 - acc: 0.8220 - val_loss: 0.7893 - val_acc: 0.7528\n",
      "Epoch 80/200\n",
      "45000/45000 [==============================] - 362s - loss: 0.5513 - acc: 0.8183 - val_loss: 0.7719 - val_acc: 0.7660\n",
      "Epoch 81/200\n",
      "45000/45000 [==============================] - 361s - loss: 0.5490 - acc: 0.8218 - val_loss: 0.7447 - val_acc: 0.7672\n",
      "Epoch 82/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5618 - acc: 0.8178 - val_loss: 0.8146 - val_acc: 0.7466\n",
      "Epoch 83/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5567 - acc: 0.8171 - val_loss: 0.8180 - val_acc: 0.7356\n",
      "Epoch 84/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5606 - acc: 0.8187 - val_loss: 0.6972 - val_acc: 0.7810\n",
      "Epoch 85/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5571 - acc: 0.8190 - val_loss: 0.7646 - val_acc: 0.7622\n",
      "Epoch 86/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5462 - acc: 0.8205 - val_loss: 0.7271 - val_acc: 0.7774\n",
      "Epoch 87/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5463 - acc: 0.8220 - val_loss: 0.7355 - val_acc: 0.7740\n",
      "Epoch 88/200\n",
      "45000/45000 [==============================] - 355s - loss: 0.5632 - acc: 0.8178 - val_loss: 0.7740 - val_acc: 0.7578\n",
      "Epoch 89/200\n",
      "45000/45000 [==============================] - 353s - loss: 0.5666 - acc: 0.8168 - val_loss: 0.7274 - val_acc: 0.7702\n",
      "Epoch 90/200\n",
      "45000/45000 [==============================] - 358s - loss: 0.5511 - acc: 0.8191 - val_loss: 0.7490 - val_acc: 0.7650\n",
      "Epoch 91/200\n",
      "45000/45000 [==============================] - 354s - loss: 0.5398 - acc: 0.8239 - val_loss: 0.7705 - val_acc: 0.7648\n",
      "Epoch 92/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5452 - acc: 0.8247 - val_loss: 0.7328 - val_acc: 0.7724\n",
      "Epoch 93/200\n",
      "45000/45000 [==============================] - 356s - loss: 0.5297 - acc: 0.8267 - val_loss: 0.7400 - val_acc: 0.7788\n",
      "Epoch 94/200\n",
      "45000/45000 [==============================] - 353s - loss: 0.5538 - acc: 0.8225 - val_loss: 0.7552 - val_acc: 0.7740\n",
      "Epoch 95/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5402 - acc: 0.8223 - val_loss: 0.7513 - val_acc: 0.7790\n",
      "Epoch 96/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5374 - acc: 0.8290 - val_loss: 0.7355 - val_acc: 0.7762\n",
      "Epoch 97/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5367 - acc: 0.8272 - val_loss: 0.7454 - val_acc: 0.7662\n",
      "Epoch 98/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5441 - acc: 0.8251 - val_loss: 0.7874 - val_acc: 0.7568\n",
      "Epoch 99/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5596 - acc: 0.8199 - val_loss: 0.7445 - val_acc: 0.7672\n",
      "Epoch 100/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5407 - acc: 0.8259 - val_loss: 0.7531 - val_acc: 0.7596\n",
      "Epoch 101/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5434 - acc: 0.8234 - val_loss: 0.7338 - val_acc: 0.7706\n",
      "Epoch 102/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5280 - acc: 0.8282 - val_loss: 0.7411 - val_acc: 0.7686\n",
      "Epoch 103/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5408 - acc: 0.8234 - val_loss: 0.7467 - val_acc: 0.7660\n",
      "Epoch 104/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5582 - acc: 0.8201 - val_loss: 0.7073 - val_acc: 0.7752\n",
      "Epoch 105/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5539 - acc: 0.8228 - val_loss: 0.7778 - val_acc: 0.7590\n",
      "Epoch 106/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5317 - acc: 0.8277 - val_loss: 0.8198 - val_acc: 0.7464\n",
      "Epoch 107/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5382 - acc: 0.8277 - val_loss: 0.8284 - val_acc: 0.7364\n",
      "Epoch 108/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5460 - acc: 0.8241 - val_loss: 0.8234 - val_acc: 0.7488\n",
      "Epoch 109/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5506 - acc: 0.8242 - val_loss: 0.7810 - val_acc: 0.7566\n",
      "Epoch 110/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5453 - acc: 0.8241 - val_loss: 0.7589 - val_acc: 0.7686\n",
      "Epoch 111/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5553 - acc: 0.8209 - val_loss: 0.7621 - val_acc: 0.7604\n",
      "Epoch 112/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5362 - acc: 0.8277 - val_loss: 0.8116 - val_acc: 0.7544\n",
      "Epoch 113/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5264 - acc: 0.8316 - val_loss: 0.7514 - val_acc: 0.7682\n",
      "Epoch 114/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5267 - acc: 0.8313 - val_loss: 0.7692 - val_acc: 0.7654\n",
      "Epoch 115/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5333 - acc: 0.8308 - val_loss: 0.7720 - val_acc: 0.7528\n",
      "Epoch 116/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5474 - acc: 0.8254 - val_loss: 0.7638 - val_acc: 0.7680\n",
      "Epoch 117/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5328 - acc: 0.8308 - val_loss: 0.7595 - val_acc: 0.7724\n",
      "Epoch 118/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5303 - acc: 0.8295 - val_loss: 0.7806 - val_acc: 0.7582\n",
      "Epoch 119/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5499 - acc: 0.8244 - val_loss: 0.7916 - val_acc: 0.7584\n",
      "Epoch 120/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5381 - acc: 0.8300 - val_loss: 0.7469 - val_acc: 0.7730\n",
      "Epoch 121/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5374 - acc: 0.8282 - val_loss: 0.7479 - val_acc: 0.7680\n",
      "Epoch 122/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5414 - acc: 0.8273 - val_loss: 0.7388 - val_acc: 0.7670\n",
      "Epoch 123/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5288 - acc: 0.8313 - val_loss: 0.7442 - val_acc: 0.7740\n",
      "Epoch 124/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5304 - acc: 0.8332 - val_loss: 0.8405 - val_acc: 0.7332\n",
      "Epoch 125/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5290 - acc: 0.8307 - val_loss: 0.7396 - val_acc: 0.7756\n",
      "Epoch 126/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5075 - acc: 0.8403 - val_loss: 0.8026 - val_acc: 0.7560\n",
      "Epoch 127/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5217 - acc: 0.8346 - val_loss: 0.7505 - val_acc: 0.7736\n",
      "Epoch 128/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5471 - acc: 0.8285 - val_loss: 0.7720 - val_acc: 0.7606\n",
      "Epoch 129/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5489 - acc: 0.8263 - val_loss: 0.7618 - val_acc: 0.7662\n",
      "Epoch 130/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5584 - acc: 0.8213 - val_loss: 0.7120 - val_acc: 0.7810\n",
      "Epoch 131/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5536 - acc: 0.8235 - val_loss: 0.8552 - val_acc: 0.7464\n",
      "Epoch 132/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5378 - acc: 0.8298 - val_loss: 0.7342 - val_acc: 0.7628\n",
      "Epoch 133/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5355 - acc: 0.8325 - val_loss: 0.7596 - val_acc: 0.7666\n",
      "Epoch 134/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5458 - acc: 0.8256 - val_loss: 0.7666 - val_acc: 0.7682\n",
      "Epoch 135/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5234 - acc: 0.8354 - val_loss: 0.7748 - val_acc: 0.7550\n",
      "Epoch 136/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5368 - acc: 0.8286 - val_loss: 0.8233 - val_acc: 0.7412\n",
      "Epoch 137/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5157 - acc: 0.8346 - val_loss: 0.7828 - val_acc: 0.7596\n",
      "Epoch 138/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5251 - acc: 0.8329 - val_loss: 0.7581 - val_acc: 0.7662\n",
      "Epoch 139/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5345 - acc: 0.8302 - val_loss: 0.7741 - val_acc: 0.7674\n",
      "Epoch 140/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5060 - acc: 0.8380 - val_loss: 0.7455 - val_acc: 0.7686\n",
      "Epoch 141/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5114 - acc: 0.8375 - val_loss: 0.7765 - val_acc: 0.7734\n",
      "Epoch 142/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5134 - acc: 0.8382 - val_loss: 0.7929 - val_acc: 0.7650\n",
      "Epoch 143/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5166 - acc: 0.8349 - val_loss: 0.7959 - val_acc: 0.7608\n",
      "Epoch 144/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5262 - acc: 0.8336 - val_loss: 0.8892 - val_acc: 0.7498\n",
      "Epoch 145/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5262 - acc: 0.8347 - val_loss: 0.7703 - val_acc: 0.7682\n",
      "Epoch 146/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5257 - acc: 0.8332 - val_loss: 0.8057 - val_acc: 0.7554\n",
      "Epoch 147/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5198 - acc: 0.8352 - val_loss: 0.7619 - val_acc: 0.7674\n",
      "Epoch 148/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5208 - acc: 0.8343 - val_loss: 0.7633 - val_acc: 0.7790\n",
      "Epoch 149/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5210 - acc: 0.8365 - val_loss: 0.8059 - val_acc: 0.7528\n",
      "Epoch 150/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5239 - acc: 0.8347 - val_loss: 0.7883 - val_acc: 0.7548\n",
      "Epoch 151/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5283 - acc: 0.8326 - val_loss: 0.8098 - val_acc: 0.7548\n",
      "Epoch 152/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5124 - acc: 0.8387 - val_loss: 0.8386 - val_acc: 0.7460\n",
      "Epoch 153/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5096 - acc: 0.8379 - val_loss: 0.8076 - val_acc: 0.7588\n",
      "Epoch 154/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5172 - acc: 0.8376 - val_loss: 0.7998 - val_acc: 0.7542\n",
      "Epoch 155/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5067 - acc: 0.8403 - val_loss: 0.8230 - val_acc: 0.7640\n",
      "Epoch 156/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4961 - acc: 0.8418 - val_loss: 0.7723 - val_acc: 0.7676\n",
      "Epoch 157/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5123 - acc: 0.8401 - val_loss: 0.7909 - val_acc: 0.7628\n",
      "Epoch 158/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5082 - acc: 0.8410 - val_loss: 0.7760 - val_acc: 0.7766\n",
      "Epoch 159/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4968 - acc: 0.8439 - val_loss: 0.7920 - val_acc: 0.7588\n",
      "Epoch 160/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5044 - acc: 0.8420 - val_loss: 0.7400 - val_acc: 0.7720\n",
      "Epoch 161/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5056 - acc: 0.8408 - val_loss: 0.7877 - val_acc: 0.7594\n",
      "Epoch 162/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5095 - acc: 0.8399 - val_loss: 0.8226 - val_acc: 0.7528\n",
      "Epoch 163/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5220 - acc: 0.8359 - val_loss: 0.7574 - val_acc: 0.7696\n",
      "Epoch 164/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5048 - acc: 0.8417 - val_loss: 0.7664 - val_acc: 0.7744\n",
      "Epoch 165/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5171 - acc: 0.8376 - val_loss: 0.8104 - val_acc: 0.7594\n",
      "Epoch 166/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5079 - acc: 0.8403 - val_loss: 0.8115 - val_acc: 0.7618\n",
      "Epoch 167/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5012 - acc: 0.8434 - val_loss: 0.7822 - val_acc: 0.7632\n",
      "Epoch 168/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4894 - acc: 0.8454 - val_loss: 0.7973 - val_acc: 0.7644\n",
      "Epoch 169/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5096 - acc: 0.8414 - val_loss: 0.7681 - val_acc: 0.7734\n",
      "Epoch 170/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5052 - acc: 0.8422 - val_loss: 0.7476 - val_acc: 0.7778\n",
      "Epoch 171/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5030 - acc: 0.8427 - val_loss: 0.7961 - val_acc: 0.7810\n",
      "Epoch 172/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5098 - acc: 0.8437 - val_loss: 0.8039 - val_acc: 0.7680\n",
      "Epoch 173/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5109 - acc: 0.8432 - val_loss: 0.8145 - val_acc: 0.7560\n",
      "Epoch 174/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5105 - acc: 0.8416 - val_loss: 0.7771 - val_acc: 0.7638\n",
      "Epoch 175/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4834 - acc: 0.8485 - val_loss: 0.8726 - val_acc: 0.7320\n",
      "Epoch 176/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.4764 - acc: 0.8498 - val_loss: 0.8013 - val_acc: 0.7522\n",
      "Epoch 177/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4971 - acc: 0.8440 - val_loss: 0.8208 - val_acc: 0.7642\n",
      "Epoch 178/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4988 - acc: 0.8451 - val_loss: 0.7690 - val_acc: 0.7670\n",
      "Epoch 179/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4908 - acc: 0.8449 - val_loss: 0.7545 - val_acc: 0.7674\n",
      "Epoch 180/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5015 - acc: 0.8445 - val_loss: 0.7919 - val_acc: 0.7574\n",
      "Epoch 181/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4914 - acc: 0.8463 - val_loss: 0.7457 - val_acc: 0.7764\n",
      "Epoch 182/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4881 - acc: 0.8480 - val_loss: 0.7632 - val_acc: 0.7774\n",
      "Epoch 183/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5030 - acc: 0.8445 - val_loss: 0.7711 - val_acc: 0.7700\n",
      "Epoch 184/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5147 - acc: 0.8386 - val_loss: 0.7915 - val_acc: 0.7610\n",
      "Epoch 185/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5089 - acc: 0.8399 - val_loss: 0.7996 - val_acc: 0.7684\n",
      "Epoch 186/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5101 - acc: 0.8412 - val_loss: 0.7986 - val_acc: 0.7744\n",
      "Epoch 187/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5082 - acc: 0.8431 - val_loss: 0.7544 - val_acc: 0.7832\n",
      "Epoch 188/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5158 - acc: 0.8400 - val_loss: 0.7931 - val_acc: 0.7660\n",
      "Epoch 189/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5154 - acc: 0.8391 - val_loss: 0.7831 - val_acc: 0.7652\n",
      "Epoch 190/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.4899 - acc: 0.8459 - val_loss: 0.7975 - val_acc: 0.7592\n",
      "Epoch 191/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4970 - acc: 0.8458 - val_loss: 0.8263 - val_acc: 0.7564\n",
      "Epoch 192/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5063 - acc: 0.8438 - val_loss: 0.8170 - val_acc: 0.7730\n",
      "Epoch 193/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5110 - acc: 0.8404 - val_loss: 0.8124 - val_acc: 0.7626\n",
      "Epoch 194/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5145 - acc: 0.8420 - val_loss: 0.8775 - val_acc: 0.7348\n",
      "Epoch 195/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5043 - acc: 0.8429 - val_loss: 0.8032 - val_acc: 0.7638\n",
      "Epoch 196/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.4904 - acc: 0.8468 - val_loss: 0.8899 - val_acc: 0.7378\n",
      "Epoch 197/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.5162 - acc: 0.8411 - val_loss: 0.8139 - val_acc: 0.7646\n",
      "Epoch 198/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.4748 - acc: 0.8522 - val_loss: 0.7815 - val_acc: 0.7588\n",
      "Epoch 199/200\n",
      "45000/45000 [==============================] - 351s - loss: 0.5095 - acc: 0.8431 - val_loss: 0.7929 - val_acc: 0.7684\n",
      "Epoch 200/200\n",
      "45000/45000 [==============================] - 352s - loss: 0.4796 - acc: 0.8510 - val_loss: 0.7995 - val_acc: 0.7722\n",
      "Testing...\n",
      "10000/10000 [==============================] - 23s    \n",
      "Test accuracy: 0.7628\n"
     ]
    }
   ],
   "source": [
    "train_model(model,X_train,Y_train,X_test,Y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 存储模型 ##"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model_json = model.to_json()\n",
    "open('cifar10_architecture.json', 'w').write(model_json)\n",
    "model.save_weights('cifar10_weights.h5', overwrite=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这样 我们训练好的模型已经可以了，下次不需要再训练，直接拿过来用就OK了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下面来使用这个模型来预测图片 ##"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 首先预处理图片 ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.misc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_and_scale_imgs():\n",
    "   img_names = ['standing-cat.jpg', ]\n",
    " \n",
    "   imgs = [np.transpose(scipy.misc.imresize(scipy.misc.imread(img_name), (32, 32)),\n",
    "                        (2, 0, 1)).astype('float32')\n",
    "           for img_name in img_names]\n",
    "   return np.array(imgs) / 255"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取模型 ##"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.models import model_from_json\n",
    " \n",
    "def load_model(model_def_fname, model_weight_fname):\n",
    "   model = model_from_json(open(model_def_fname).read())\n",
    "   model.load_weights(model_weight_fname)\n",
    "   return model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始预测 ##"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "imgs = load_and_scale_imgs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n"
     ]
    }
   ],
   "source": [
    "predictions = model.predict_classes(imgs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3]\n"
     ]
    }
   ],
   "source": [
    "print(predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predictImage(url):\n",
    "    img_names = [url, ]\n",
    "    imgs = [np.transpose(scipy.misc.imresize(scipy.misc.imread(img_name), (32, 32)),\n",
    "                        (2, 0, 1)).astype('float32')\n",
    "           for img_name in img_names]\n",
    "    imgs = np.array(imgs) / 255\n",
    "    predictions = model.predict_classes(imgs)\n",
    "    print predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n",
      "[3]\n"
     ]
    }
   ],
   "source": [
    "predictImage('cat1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n",
      "[5]\n"
     ]
    }
   ],
   "source": [
    "predictImage('images.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n",
      "[2]\n"
     ]
    }
   ],
   "source": [
    "predictImage('index.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可见，判断率"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
